﻿$(function () {
    $.kish.carAnnDetails = {};
    $.extend($.kish.carAnnDetails, {

        //********************************//
        //set basid details
        initializing: function () {

            var carID = $.Jahanutility.getParameterByName("ID");
            if (carID != "0" && carID != '') {
                $.kish.carAnnDetails.ShowDetails(carID);
            };
            $.Jahanutility.togglePanels($("#CarSearchHeader"), $(".CarSearchHeader"));
            $.Jahanutility.togglePanels($("#carstoreSearchHeader"), $(".carstoreSearchHeader"));
            $.Jahanutility.togglePanels($("#serivceSearchHeader"), $(".serivceSearchHeader"));
        },
        //****************************//
        ShowDetails: function (carID) {

            // $('#submit').before('<img src="../../images/ajax-loader.gif" class="loader" />')            

            $('#submit').attr('disabled', 'disabled');
            $.ajax({
                'type': 'POST',
                'url': '../../KishCar/Services/CarAnn.svc/GetCarDetilas',
                'data': JSON.stringify({ CarID: carID }),
                'contentType': 'application/json; charset=utf-8',
                'dataType': 'json',
                'success': function (result) {
                    var CarData = result['d'].Car;
                    var RelatedData = result['d'].Related;

                    $(".price").text($.trim(CarData.Price));
                    $(".year").text($.trim(CarData.ProductYear));
                    $(".usage").text($.trim(CarData.TraveledDistance));
                    $(".commentTxt").text($.trim(CarData.Description));
                    $(".model").text($.trim(CarData.Model.Code));
                    $(".color").text($.trim(CarData.Color.Code));
                    $(".brand").text($.trim(CarData.Model.Brand.Code));
                    $(".state").text($.trim(CarData.Status.Code));
                    $.each(CarData.CarImages, function () {
                        var relatedPath = '../' + this.Path.replace('~', '..');
                        var Image = '<a  class="highslide inline-block margin_l_15" href="' + relatedPath + '"> <img  class="width120 fn_prodImg " alt="' + CarData.Model.Code + '" title="تصویر بزرگتر" src= "' + relatedPath + '"/></a>';
                        $('.CarImagesDetail').append(Image)
                    });

                    $.kish.carAnnDetails.GetRelated(RelatedData);

                    $('#message').slideDown();
                    $('img.loader').fadeOut('slow', function () { $(this).remove() });
                    $('#submit').removeAttr('disabled', 'disabled');

                    $('a.highslide').click(function () {
                        return hs.expand(this)
                    });
                },
                'error': function (error) {
                    $('#submit').removeAttr('disabled');
                    $('img.loader').fadeOut('slow', function () { $(this).remove() });
                }
            });
        },
        GetRelated: function (RelatedData) {
            //*************Fill top cars announcement *************
            if (RelatedData.Car != null && RelatedData.Car.length > 0) {
                $('#carResult').html('');
                for (var i = 0; i < RelatedData.Car.length; i++) {
                    var price = $.trim(RelatedData.Car[i].Price);
                    var path = '../Images/carNoPhoto.gif';
                    if (RelatedData.Car[i].CarImages[0] != null) {
                        var relatedPath = (RelatedData.Car[i].CarImages[0].Path);
                        path = '../' + relatedPath.replace('~', '..');
                    }
                    var x = '<div class="three columns isotope-item ">' +
                                        '<a class="portfolio-item isotope " href="CarDetails.aspx?ID=' + RelatedData.Car[i].ID + '">' +
                                            '<figure>' +
                                            '<div class="crop">' +
                                                '<img alt="" src="' + path + '">' +
                                            '</div>' +
						                        '<figcaption class="item-description">' +
							                        '<h6>' + RelatedData.Car[i].Model.Code + ' ' + RelatedData.Car[i].Model.Brand.Code + '</h6>' +
                    //							                        '<span>' + $.trim(RelatedData[i].Color.Code) + '</span>' + ' ' +
                    //                                                    '<span>' + $.trim(RelatedData[i].ProductYear) + '</span>' +
                                                  '<span >تومان </span>' + '<span>' + price + '</span>' +
                                                 '</figcaption>' +
					                       '</figure>' +
                                        '</a>' +
                                    '</div>';
                    $('#carResult').append(x);
                };
            };

            //*************Fill top stores announcement *************
            if (RelatedData.CarStore != null && RelatedData.CarStore.length > 0) {
                $('#carstoreResult').html('');
                for (var i = 0; i < RelatedData.CarStore.length; i++) {
                    var tel = $.trim(RelatedData.CarStore[i].Telephone);
                    var path = '../Images/carstoreNoPhoto.gif';
                    if (RelatedData.CarStore[i].File != null) {
                        var relatedPath = (RelatedData.CarStore[i].File.Path);
                        path = '../' + relatedPath.replace('~', '..');
                    }
                    var x = '<div class="three columns isotope-item ">' +
                                        '<a class="portfolio-item isotope " href="CarDetails.aspx?ID=' + RelatedData.CarStore[i].ID + '">' +
                                            '<figure>' +
                                            '<div class="crop">' +
                                                '<img alt="" src="' + path + '">' +
                                            '</div>' +
						                        '<figcaption class="item-description">' +
							                        '<h6>' + RelatedData.CarStore[i].Name + '</h6>' +
                                                (tel.length > 0 ? '<span > تلفن </span>' + '<span>' + tel + '</span>' : '') +
                                                 '</figcaption>' +
					                       '</figure>' +
                                        '</a>' +
                                    '</div>';
                    $('#carstoreResult').append(x);
                };
            };
            //*************Fill top  services announcement *************
            if (RelatedData.ServiceCenter != null && RelatedData.ServiceCenter.length > 0) {
                $('#serviceResult').html('');
                for (var i = 0; i < RelatedData.ServiceCenter.length; i++) {
                    var tel = $.trim(RelatedData.ServiceCenter[i].Telephone);

                    var path = '../Images/serviceNoPhoto.gif';
                    if (RelatedData.ServiceCenter[i].File != null) {
                        var relatedPath = (RelatedData[i].ServiceCenter.File.Path);
                        path = '../' + relatedPath.replace('~', '..');
                    }
                    var x = '<div class="three columns isotope-item ">' +
                                        '<a class="portfolio-item isotope " href="CarDetails.aspx?ID=' + RelatedData.ServiceCenter[i].ID + '">' +
                                            '<figure>' +
                                            '<div class="crop">' +
                                                '<img alt="" src="' + path + '">' +
                                            '</div>' +
						                        '<figcaption class="item-description">' +
							                        '<h6>' + RelatedData.ServiceCenter[i].Name + '</h6>' +
                                                     (tel.length > 0 ? '<span > تلفن </span>' + '<span>' + tel + '</span>' : '') +
                                                '</figcaption>' +
					                       '</figure>' +
                                        '</a>' +
                                    '</div>';
                    $('#serviceResult').append(x);
                };
            };
        }

    });
    $.kish.carAnnDetails.initializing();
});



